package com.ren.movie.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ren.bean.OrderDto;
import com.ren.movie.bean.Movie;
import com.ren.movie.bean.Ticket;
import com.ren.service.IOrderService;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MqService {
    @Autowired
    ITicketService ticketService;
    @Autowired
    IMovieService movieService;
    @Autowired
    IOrderService orderService;
    @RabbitListener(queues = "queueOne001")
    public void getObject(OrderDto orderDto){
        System.out.println("orderDto = " + orderDto);
        int num = orderDto.getNum();
        String ticketname = orderDto.getTicketname();
        QueryWrapper<Ticket> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("ticketName", ticketname);
        Ticket one = ticketService.getOne(queryWrapper);
        Movie movie=new Movie();
        BeanUtils.copyProperties(orderDto,movie);
        if(one.getTicketnum()>=num){
            one.setTicketnum(one.getTicketnum()-num);
            boolean save = ticketService.saveOrUpdate(one);
            save = movieService.save(movie);
            boolean b = orderService.saveOrder(1, orderDto.getOrderid());
        }else {
            boolean b = orderService.saveOrder(2, orderDto.getOrderid());
        }
    }
}
